<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2017/6/8
 * Time: 下午4:12
 */

/*
 * set_error_handler(function ($errno, $errmsg, $filename, $linenum, $vars) {
    echo '$errno : ' . $errno . PHP_EOL;
    echo '$errmsg : ' . $errmsg . PHP_EOL;
    echo '$filename : ' . $filename . PHP_EOL;
    echo '$linenum : ' . $linenum . PHP_EOL;
});
*/


set_error_handler(function (...$a) {
    foreach ($a as $k => $v) {
        var_dump($v);
    }
});
trigger_error(__FUNCTION__);


function fromArray(XMLWriter $xml, $prm_array)
{
    if (is_array($prm_array)) {
        foreach ($prm_array as $index => $element) {
            if (is_array($element)) {
                $xml->startElement($index);
                fromArray($xml, $element);
                $xml->endElement();
            } else {
                $xml->startElement($index);
                $xml->text($element);
                $xml->endElement();
            }

        }
    }
}

$xml = new XMLWriter();
$xml->openMemory();
$xml->setIndent(true);
$xml->setIndentString('  ');
$xml->startDocument('1.0.0', 'UTF-8');
$xml->startElement("root");
fromArray($xml, [
    'page_title' => 'Generate a XHTML page from XML+XSLT files',
    'welcome_msg' => 'Simple XHTML document from XML+XSLT files!',
    'prova' => array(
        "gino" => array(
            "innergino" => "gino inner value"
        ),
        "filo" => "filodata"
    ),
]);
$xml->endElement();
$xml->endDocument();
$output = $xml->outputMemory(true);

//echo $output;
error_log($output,3,'msg.xml');

//user_error(__FUNCTION__);